package com.leetcode;

public class SumRootToLeaf {
	public int sumNumbers(TreeNode root) {
        if (root == null) return 0;
        return calc(root, 0);
    }
	
	private int calc(TreeNode node, int base) {
		int cur = base*10 + node.val;
        if (node.left == null && node.right == null) {
			return cur;
		}
		
        int total = 0;
		if (node.left != null) total += calc(node.left, cur);
		if (node.right != null) total += calc(node.right, cur);
        return total;
	}
	
	private class TreeNode {
		TreeNode left;
		TreeNode right;
		int val;
		public TreeNode(int val) {
			this.val = val;
		}
	}
}
